package com.zby.big_event_zby.controller;

import com.zby.big_event_zby.pojo.Result;
import com.zby.big_event_zby.pojo.User;
import com.zby.big_event_zby.service.LoginService;
import com.zby.big_event_zby.service.UserService;
import com.zby.big_event_zby.utils.JwtUtils;
import com.zby.big_event_zby.utils.Md5Util;
import com.zby.big_event_zby.utils.ThreadLocalUtils;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @auther ZBY
 */
@RestController
@RequestMapping("/user")
@Validated
@CrossOrigin(origins = "http://127.0.0.1:5173")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private LoginService loginService;
    //注册
    @PostMapping("/register")
public Result register(@Pattern(regexp = "^[a-zA-Z0-9_-]{5,12}$",message = "用户名格式错误")
                        String username, @Pattern(regexp = "^[a-zA-Z0-9_-]{6,12}$",message = "密码格式错误")String password){
//        //判断用户名是否为5-12位
//        if (username.length()<5||username.length()>12){
//            return Result.error("用户名长度必须为5-12位");
//        }
//        //判断密码是否为6-12位
//        if (password.length()<6||password.length()>12){
//            return Result.error("密码长度必须为6-12位");
//        }

        //查询用户
        User u = userService.findByUsername(username);
    //判断用户是否存在
        if (u==null){
            //存在创建用户
            userService.register(username,password);
            return Result.success();

        }else {
            //存在返回错误信息
            return Result.error("用户名已存在");
        }
    };
    //登录
    @PostMapping("/login")
    public Result<String> login(@Pattern(regexp = "^[a-zA-Z0-9_-]{5,12}$",message = "用户名格式错误")String username,
                                @Pattern(regexp = "^[a-zA-Z0-9_-]{5,12}$",message = "用户名格式错误")String password){
        //判断用户是否存在和判断密码是否正确
        User login = loginService.login(username, password);


        if(login!=null&&login.getUsername().equals(username)&&  Md5Util.getMD5String(password).equals(login.getPassword())){
            //以id和username为token
            Map<String, Object> map = new HashMap<>();
            map.put("id",login.getId());
            map.put("username",login.getUsername());

            String token = JwtUtils.getToken(map);

            return Result .success(token);
        }
        return Result.error("用户名或密码错误");


    }
//    获取用户信息
    @GetMapping("/userInfo")
    public Result<User> getUserInfo(@RequestHeader(name = "Authorization") String token){
//        Map<String, Object> map = JwtUtils.pareseToken(token);
//        String user =(String) map.get("username");

        Map<String, Object> map = ThreadLocalUtils.get();
        String  username = (String) map.get("username");
        User byUsername = userService.findByUsername(username);
        return Result.success(byUsername);

    }
    //更改用户信息
    @PutMapping("/updata")
    public Result update(@RequestBody @Validated User user){


        userService.update(user);
    return Result.success();


    }
    @PatchMapping("/updateAvatar")
    public Result UpdateAvatarUrl(@RequestParam @URL String avatarUrl){
        userService.updateAvatarUrl(avatarUrl);
        return Result.success();
    }
    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String,String> params){
       // 获取参数
       String oldPwd = params.get("old_pwd");
        String newPwd = params.get("new_pwd");
        String rePwd = params.get("re_pwd");
      if (!(StringUtils.hasLength(oldPwd)|| StringUtils.hasLength(newPwd)||StringUtils.hasLength(rePwd))){

          return Result.error("参数不能为空");
      }
        //检验密码

        //获取用户姓名存入map集合中
        Map<String, Object> map = ThreadLocalUtils.get();
        //取出用户名
        String username = (String)map.get("username");
        //查询用户
        User byUsername = userService.findByUsername(username);
        //判断旧密码是否正确
        if (!Md5Util.getMD5String(oldPwd).equals(byUsername.getPassword())){
            return Result.error("旧密码错误");
        }
        //判断两次密码是否相等
        if (!newPwd.equals(rePwd)){
            return Result.error("两次密码不一致");
        }
        //调用service层进行更新密码
        userService.updatePwd(newPwd);
        return Result.success();
    }
}
